﻿using System;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Web;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.WebControls;
using NotAClue.Web.DynamicData;
using NotAClue.Web;

namespace DD_Extensions
{
    public partial class Boolean_EditField : ParentChangeNotifyingFieldTemplate //System.Web.DynamicData.FieldTemplateUserControl
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            // add tool-tip
            this.SetupTip();

            // set field options
            this.SetFieldOptions();

            // add CSS from meta data
            this.ApplyCss();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.HasEvents)
                CheckBox1.AutoPostBack = true;
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            // raise event
            RaiseStatusChanged(new ListItem(CheckBox1.Checked.ToString()));

            CheckBox1.SetFocusHere();
        }

        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);

            object val = FieldValue;
            if (val != null)
                CheckBox1.Checked = (bool)val;


            if (Column.IsRequired)
                CheckBox1.CssClass += " DDRequired";

            // set initial state
            SetState(new ListItem(CheckBox1.Checked.ToString()));
        }

        protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            dictionary[Column.Name] = CheckBox1.Checked;
        }

        public override Control DataControl
        {
            get { return CheckBox1; }
        }
    }
}
